package com.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

//获取类的信息
public class Test07 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 = Class.forName("com.reflection.User");

        //获取类的名字
        System.out.println(c1.getName()); //com.reflection.User
        System.out.println(c1.getSimpleName());  //User

        System.out.println("----------------------------");
        //获取类的属性
        Field[] fields = c1.getFields();  //只能找到public属性
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println("=================================");
        Field[] declaredFields = c1.getDeclaredFields(); // 找到全部属性
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }

        //获得指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);

        System.out.println("------------------------------");
        //获得类的方法
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println("正常的：" + method);
        }

        methods = c1.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("getDeclaredMethods：" + method);
        }

        System.out.println("------------------------------");
        //获得指定方法
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);
    }
}
